import request from '@/utils/request'

export interface LoginRes {
  token: string
}

export interface UserState {
  uid?: number
  name?: string
  avatar?: string
  username?: string
  email?: string
  phone?: string
  gender?: number
  birthday?: string
  address?: string
  signature?: string
}

/**
 * 获取用户信息
 */
export function getUserInfo() {
  return request({
    url: '/user/userInfo',
    method: 'get'
  })
}

/**
 * 更新用户信息
 * @param data 用户信息
 */
export function updateUserInfo(data: Partial<UserState>) {
  return request({
    url: '/api/user/update',
    method: 'put',
    data
  })
}

/**
 * 上传头像
 * @param data 图片文件数据
 */
export function uploadAvatar(data: FormData) {
  return request({
    url: '/api/user/avatar',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 修改密码
 * @param data 密码信息
 */
export function changePassword(data: {
  oldPassword: string
  newPassword: string
  confirmPassword: string
}) {
  return request({
    url: '/api/user/password',
    method: 'put',
    data
  })
}

/**
 * 绑定手机号
 * @param data 手机信息
 */
export function bindPhone(data: {
  phone: string
  code: string
}) {
  return request({
    url: '/api/user/bind-phone',
    method: 'post',
    data
  })
}

/**
 * 发送手机验证码
 * @param phone 手机号
 */
export function sendPhoneCode(phone: string) {
  return request({
    url: '/api/user/send-phone-code',
    method: 'post',
    data: { phone }
  })
}

/**
 * 绑定邮箱
 * @param data 邮箱信息
 */
export function bindEmail(data: {
  email: string
  code: string
}) {
  return request({
    url: '/api/user/bind-email',
    method: 'post',
    data
  })
}

/**
 * 发送邮箱验证码
 * @param email 邮箱
 */
export function sendEmailCode(email: string) {
  return request({
    url: '/api/user/send-email-code',
    method: 'post',
    data: { email }
  })
}

/**
 * 获取用户收藏列表
 * @param params 查询参数
 */
export function getUserFavorites(params: {
  page: number
  limit: number
  type?: string
}) {
  return request({
    url: '/api/user/favorites',
    method: 'get',
    params
  })
}

/**
 * 获取用户足迹列表
 * @param params 查询参数
 */
export function getUserFootprints(params: {
  page: number
  limit: number
  type?: string
}) {
  return request({
    url: '/api/user/footprints',
    method: 'get',
    params
  })
}


export function getaboutUs(data?:any): Promise<any> {
  return request.get('home/aboutUs', {params:data})
}


export function todaySign(data?:any): Promise<any> {
  return request.post('user/todaySign', data)
}

export function vipdetail(data?:any): Promise<any> {
  return request.get('user/vipdetail', {params:data})
}

export function addWithdrawal(data?:any): Promise<any> {
  return request.post('account/addWithdrawal',data)
}

export function getMyTeam(data: {
  page: string
  limit: string
  type: '1' | '2' | '3' | '4' | '5'| '6' // 1:b 2:c 3:d 4:e 5:f 6:g
  isall: string //Invalid: 0  Valid: 1
}): Promise<any> {
  return request.get('user/getMyTeam', {params:data})
}

export function getMyTeamInfo(data?:any): Promise<any> {
  return request.get('user/getMyTeamInfo', {params:data})
}

/**
 * 退出登录
 */
export function logout() {
  return request({
    url: '/user/logout',
    method: 'post'
  })
}

/**
 * 修改昵称
 * @param value 新昵称
 */
export function editNickname(value: string) {
  return request({
    url: '/user/editNikeName',
    method: 'post',
    data: { value }
  })
}

/**
 * 修改密码
 * @param data 修改密码参数
 */
export function editPassword(data: {
  oldPassword: string
  newPassword: string
  type: 'LOGIN' | 'TEL' | 'EMAIL'
}) {
  return request({
    url: '/user/editPassword',
    method: 'post',
    data
  })
}

/**
 * 获取用户银行卡信息
 * 返回数据格式:
 * - bankCard: 银行账户
 * - userNmae: 姓名
 * - khyh: 开户银行
 */
export function getUserBankInfo() {
  return request({
    url: '/user/useBankInfo',
    method: 'get'
  })
}

/**
 * 编辑用户银行卡信息
 * @param data 银行卡信息对象
 */
export function editUserBankCard(data: {
  userName: string  // 姓名
  khyh: string      // 开户银行
  bankCard: string  // 银行账户
  idCard: string
}) {
  return request({
    url: '/user/editUserBankCard',
    method: 'post',
    data
  })
}

/**
 * 礼品兑换/红包兑换
 * @param redCode 兑换码
 */
export function redeemGift(redCode: string) {
  return request({
    url: '/user/redExchange',
    method: 'post',
    data: { redCode }
  })
}

/**
 * 获取红包兑换记录
 * @param params 分页参数
 * @returns 返回红包兑换列表数据
 * 数据格式: {
 *   number: string  // 领取金额
 *   wdate: string   // 兑换时间
 *   redCode: string // 兑换码
 * }
 */
export function getGiftRecords(params: { page: number, limit: number }) {
  return request({
    url: '/user/redExchangeList',
    method: 'get',
    params
  })
}


/**
 * 资产记录 
 *@param type 1收入 2支出 3笔记
 * @return [
{
"number": "4122", //收益数量
"writedate": "12:29 12/05/2025", //领取
"source": "--"
}
]
 */
export function financeList(data?:{type:string, page:string, limit:string}): Promise<any> {
  return request.get('account/financeList', {params:data})
}



export function financeByType(data?:{type:string, page:string, limit:string}): Promise<any> {
  return request.get('account/financeByType', {params:data})
}


export function withdrawalTips(): Promise<any> {
  return request.get('account/withdrawalTips')
}

export function getMyIncomeList(): Promise<any> {
  return request.get('user/getMyIncomeList')
}

export function addRecharge(data: { amount: string | number }) {
  return request({
    url: 'account/addRecharge',
    method: 'post',
    data
  })
}

export function newslistbyid(data?:{id:string}): Promise<any> {
  return request.get('home/newslistbyid', {params:data})
}

export function bankList(): Promise<any> {
  return request.get('user/bankList')
}
